rm(list = setdiff(ls(), lsf.str())) 

load("Study 1/Altered Data/Study1_Swiss_Household09.RData")
#SI A.8 results belonging to Figure 1
vote.swiss.09<-list()
vote.swiss.09[[1]] <- glm(vote_svp ~agre + open + con + ext + neu + female + age + language2 + language3, data=data, family=binomial)
vote.swiss.09[[2]] <- glm(vote_svp ~agre + open + con + ext + neu + female + age + language2 + language3 + edu2 + edu3 + edu4 + edu5 + edu6 + edu7 + edu8 + edu9 + income + income_mis + zero1(anti_immi) + zero1(econ_cons) , data=data, family=binomial)

labels <- c("Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism", "Female", "Age" , "Language (ref: French): German", "Language: Italian",  "2: Lower secondary", "3A: Upper secondary", "3B: Upper secondary", "3C: Upper secondary", "4: Post-secondary education", "5A: First stage of tertiary education", "5A: First stage of tertiary education", "6: Second stage of tertiary education ", "Income", "Income missing (0-1)", "Social conservatism", "Economic conservatism")
stargazer2(vote.swiss.09, odd.ratio = TRUE, title="Swiss Household Panel 2009: Vote for Swiss People's Party", align=TRUE, omit.stat=c("LL","ser","f", "adj.rsq"),  star.cutoffs=c(0.05), covariate.labels = labels,dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "tiny",column.labels = c("Base", "Figure 1"), notes = "Odds ratios and standard errors from logistic regression models; *p<0.05",  notes.append = FALSE, out="Tables/Swiss09_results.tex", no.space=TRUE, label="tab:Swiss09_results" )

#calculate 1 SD above and below the mean
low  <- mean(zero1(data$agre), na.rm=T)-sd(zero1(data$agre), na.rm=T)
high <- mean(zero1(data$agre), na.rm=T)+sd(zero1(data$agre), na.rm=T)

#odds ratio
odds.difference <- (exp(vote.swiss.09[[2]]$coefficients)[2]*low) / (exp(vote.swiss.09[[2]]$coefficients)[2]*high)
#1/odds.difference

#SI A.8 Correlation matrix----------------------
myvars <- c("agre", "open", "con", "ext", "neu" , "anti_immi" , "econ_cons")
cor_ivs <- data[myvars]
names(cor_ivs) <- c("1. Agreeableness","2. Opennesss","3. Conscientiousness",
                    "4. Extraversion", "5. Neuroticism", 
                    "6. Social conservatism", "7. Economic conservatism")

correlation.matrix <- cor(cor_ivs, use="complete.obs")
correlation.matrix <- data.frame(get_lower_tri(correlation.matrix))
correlation.matrix<-correlation.matrix[,c(1:6)]
names(correlation.matrix) <- seq(1,6,1)

correlation.matrix<-as.matrix(correlation.matrix)
stargazer(correlation.matrix, title="Swiss Household Panel 2009: Correlation Matrix of Independent Variables", out="Tables/Swiss09_cor.tex", no.space=TRUE, label="tab:Swiss_cor09", digits=2)

#SI A.8 Descriptive statistics-----
desc.labels <- c("Populist Vote",labels)
stargazer(vote.swiss.09[[2]]$model, type = "latex", covariate.labels=desc.labels,summary.stat = c("mean", "sd", "N", "median","min",  "max"), title="Descriptive statistics Swiss Household Panel 2009", out="Tables/Swiss_descrip09.tex", no.space=TRUE, label="tab:Swiss_descriptives09", digits=2)

#Alpha Agreeableness
psych::alpha(with(data,data.frame(a2_fault,a1_trust)),na.rm=T)

#Alpha Openness
psych::alpha(with(data,data.frame(o1_artistic,o1_imagination)),na.rm=T)

#Alpha Conscieniousness
psych::alpha(with(data,data.frame(c2_lazy,c1_efficient)),na.rm=T)

#Alpha Extraversion
psych::alpha(with(data,data.frame(e1_sociable,e1_reserved)),na.rm=T)

#Alpha Neuroticism
psych::alpha(with(data,data.frame(n1_relaxed,n1_nervous)),na.rm=T)
